package com.yyy.controller;

import com.yyy.common.util.PageResult;
import com.yyy.common.util.R;
import com.yyy.entity.Employee;
import com.yyy.service.IEmployeeService;
import com.yyy.vo.EmployeeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yyy
 * @since 2025-01-10
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("/list")
    public R getStudent() {
        return R.ok().data("list", employeeService.list());
    }

    @GetMapping("/findByPage/{page}/{pageSize}")
    public R findByPage(@PathVariable("page") Integer page,
                        @PathVariable("pageSize") Integer pageSize) {
        PageResult<Employee> pr = employeeService.findByPage(page, pageSize);
        return R.ok().data("pr", pr);
    }

    @PostMapping("/search/{page}/{pageSize}")
    public R search(@PathVariable("page") Integer page,
                    @PathVariable("pageSize") Integer pageSize,
                    @RequestBody(required = false) EmployeeVO vo) {
        PageResult<Employee> pr = employeeService.search(page, pageSize, vo);
        return R.ok().data("pr", pr);
    }

    @PostMapping("/add")
    public R add(@RequestBody Employee student) {
        boolean flag = employeeService.add(student);
        return flag ? R.ok().message("添加员工成功!") : R.error().message("添加员工失败!");
    }

    @PutMapping("/update")
    public R update(@RequestBody Employee student) {
        boolean flag = employeeService.updateById(student);
        return flag ? R.ok().message("修改员工成功!") : R.error().message("修改员工失败!");
    }

    @DeleteMapping("/delete/{ids}")
    public R delete(@PathVariable("ids") String ids){
        String[] split = ids.split(",");
        List<String> sids = Arrays.asList(split);
        return employeeService.removeByIds(sids) ? R.ok().message("删除员工成功!") : R.error().message("删除员工失败!");
    }

}
